package com.zzk.lanouhn.myapplication1.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.FindXiaotuTravelsAdapter;
import com.zzk.lanouhn.myapplication1.modles.FindDetailsXiaotuModle;

import org.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/10.
 */
public class FindDetailsXiaotuTravels extends Fragment {
    private XRecyclerView xRecyclerView;

    private FindXiaotuTravelsAdapter travelsAdapter;
    private List<FindDetailsXiaotuModle.TripsBean> mlist = new ArrayList<>();
    private List<FindDetailsXiaotuModle.TripsBean> templist = new ArrayList<>();

    private int page = 1;

    //接受id
    private SharedPreferences sp;
    private String ID;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find_xiaotu_travels, null);

        xRecyclerView = (XRecyclerView) view.findViewById(R.id.find_xiaotu_travels_xrecyclerView);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(llm);
        //上拉加载风格
        xRecyclerView.setRefreshProgressStyle(22);
        //下来加载动画
        xRecyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getDataTravels();
            }

            @Override
            public void onLoadMore() {
                page++;
                getDataTravels();
            }
        });
        //接受值
        sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
        ID = sp.getString("xiaotu_youji", "55");



        getDataTravels();
        return view;
    }

    private void getDataTravels() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://chanyouji.com/api/users/" + ID + ".json?page=" + page;
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String ss = response.body().string();
                        Gson gson = new Gson();


                        FindDetailsXiaotuModle f = gson.fromJson(ss, FindDetailsXiaotuModle.class);
                        templist = f.getTrips();

                        if (templist.size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }
                    }
                });

            }
        }).start();
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "小图请求失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    if (page == 1) {
                        mlist.clear();
                        mlist.addAll(templist);
                        travelsAdapter = new FindXiaotuTravelsAdapter(getActivity(), mlist);
                        xRecyclerView.setAdapter(travelsAdapter);

                        xRecyclerView.refreshComplete();
                        templist.clear();
                    } else {
                        mlist.addAll(templist);
                        travelsAdapter.notifyDataSetChanged();
                        xRecyclerView.loadMoreComplete();
                        templist.clear();
                    }
                    break;
                case 2:
                    Toast.makeText(getActivity(), "加载到底了", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

}
